<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>资金运作凭证管理</title>
</head>
<body>	
	<script type="text/javascript">
	    
		var zjpzAddGrid;
		var data;
		$(function() {
			$.ajax({  
	            type : "post",  
	            async : false,  
	            url : "${pageContext.request.contextPath}/business/accounting/kMSZJBSJ/dataGrid.shtml",
	            success : function(res) {  
	            	 data = JSON.parse( res ); 
	            	 data=data.rows;
	            }	 
	          }),
			//$("#btnAdd").click(function () { addRow(); }).linkbutton({ iconCls: 'icon-add', plain: true });
			zjpzAddGrid =$("#zjpzAddGrid").datagrid({
				url : '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/flGrid.shtml',
				fit : true,
				border : false,
				pagination : true,
				idField : '凭证id',
				pageSize : 10,
				pageList : [ 10, 20, 30, 40, 50 ],
				sortName : 'ID',
				sortOrder : 'ASC',
				checkOnSelect : false,
				selectOnCheck : false,
				nowrap : false,
				striped : true,
				rownumbers : true,
				singleSelect : true,
				
				columns : [ [
					{
						field : 'ID',
						title : '编号',
						width : 150,
						hidden : true 
					},
					{
						field : 'action',
						title : '操作',
						align:'center',
						width : 200,
						formatter : function(value, row, index)  { 
			                   var s = '<a href="#" onclick="addRow(this)">增加凭证分录</a> ';
			                   var c = '<a href="#" onclick="cancelRow(this)">删除凭证分录</a>';
								
			                   if(index == 0){
			                	   return s+c;
			                   }else{	   
			                	   return  null;
			                   }
			           }
					},
					{
					field : 'FLXH',
					title : '分录序号',
					width : 100,
					sortable : true
					
				},
				{
					field : 'ZHAIYAO',
					title : '摘要',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 200,
					sortable : true
				} 
				, {
					field : 'KMBH',
					title : '科目编号',
					width : 80,
					sortable : true,
					
				},
				 {
					field : 'KMMC',
					title : '科目名称',
					editor:{type:'combobox',options:{valueField:'KMMC',textField:'KMMC',data:data,required:true,
						missingMessage: '请选择类别',
						onSelect:function(rec){
							var row = $('#zjpzAddGrid').datagrid('getSelected'); 
							var rowIndex = $('#zjpzAddGrid').datagrid('getRowIndex',row);//获取行号 
							row.KMBH = rec.KMBH;
							row.KMMC = rec.KMMC;
							row.FLXH = rowIndex+1;
							$('#zjpzAddGrid').datagrid('updateRow',{
								index: rowIndex,
								row:row
							});
						}
						}
					},
					width : 150,
					sortable : true
				}
				,{
					field : 'JFFSE',
					title : '借方发生额',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 150,
					sortable : true
				}, {
					field : 'DFFSE',
					title : '贷方发生额',
					editor: {
	                    type: "text",
	                    options: {
	                        required: true
	                    }
	                },
					width : 150,
					sortable : true
				}
				]], 
				 onClickRow: onClickRow,
			});
			
			console.info(zjpzAddGrid);
			zjpzAddGrid.datagrid('resize');
		});
		
		
		/* 改变操作 */
		/*  function updateActions(index){
		        $('#zjpzAddGrid').datagrid('updateRow',{
		            index: index,
		            row:{}
		        });
		  } */
		
		
		/*  function getRowIndex(target){
		        var tr = $(target).closest('tr.datagrid-row');
		        return parseInt(tr.attr('datagrid-row-index'));
		    }
		 
		    function editrow(target){
		        $('#zjpzAddGrid').datagrid('beginEdit', getRowIndex(target));
		    } */

		    
				/*删除一行*/
				
			    function cancelRow(target){
		    		if (editIndex == null) { return }
		    		parent.$.messager.confirm('询问', '您是否要删除当前选择数据？', function(b) {
		    			
		    			var id = ($('#zjpzAddGrid').datagrid("getSelected").ID);
		    			
						if (b && id!= null) {
						$.post('${pageContext.request.contextPath}/business/accounting/zJYZPZGL/deleteFL.shtml?id='+id, 
							function(result) {
							if (result.success) {
								parent.$.messager.alert('提示', result.msg, 'info');
								zjpzAddGrid.datagrid('reload');
							}
							parent.$.messager.progress('close');
							}, 'JSON');
						}else{
							$('#zjpzAddGrid').datagrid('cancelEdit', editIndex).datagrid('deleteRow', editIndex);
						}
				});
			        editIndex = null;
			    }	
				

		    
		    /* function  */
		
		// 判断时候存在编辑中的行
		var editIndex = null;
		function endEditing() {
		    if (editIndex == null) { return true }
		    if ($('#zjpzAddGrid').datagrid('validateRow', editIndex)) {
		        $('#zjpzAddGrid').datagrid('endEdit', editIndex);
		        editIndex = null;
		        return true;
		    } else {
		        return false;
		    }
		}
		// datagrid行点击事件
		function onClickRow(index, row) {
		    if (editIndex != index) {
		        if (endEditing()) {
		            $("#zjpzAddGrid").datagrid("selectRow", index).datagrid("beginEdit", index);
		            editIndex = index;
		        } else {
		            $("#zjpzAddGrid").datagrid("selectRow", editIndex);
		        }
		    }
		}
		
		 // 添加一行
		function addRow(target) {
		    if (endEditing()) {
		      //  $("#zjpzGrid").datagrid("appendRow");
		      editIndex = $("#zjpzAddGrid").datagrid("getRows").length - 1;
		      	$("#zjpzAddGrid").datagrid("appendRow", {
		      		/* "FLXH": editIndex +1, */
		      		
		        }); 
		
		        $("#zjpzAddGrid").datagrid("selectRow", editIndex).datagrid("beginEdit", editIndex);
		    }
		}
		

		

 
		
		function deleteFun(id) {
			if (id == undefined) {
				var rows = zjpzGrid.datagrid('getSelections');
				id = rows[0].ID;
			}
			parent.$.messager.confirm('询问', '您是否要删除当前选择数据？', function(b) {
				if (b) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post('${pageContext.request.contextPath}/business/accounting/kMSZJBSJ/delete.shtml', {
						id : id
					}, function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
							zjpzGrid.datagrid('reload');
						}
						parent.$.messager.progress('close');
					}, 'JSON');
				}
			});
		}
	
		function editFun(id) {
			if (id == undefined) {
				var rows = zjpzGrid.datagrid('getSelections');
				id = rows[0].ID;
			}
			parent.$.modalDialog({
				title : '编辑科目',
				width : 780,
				height : 500,
				href : '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/editPage.shtml?id=' + id,
				buttons : [ {
					text : '编辑',
					handler : function() {
						parent.$.modalDialog.openner_zjpzGrid = zjpzGrid;//因为添加成功之后，需要刷新这个zjpzGrid，所以先预定义好
						var f = parent.$.modalDialog.handler.find('#form');
						f.submit();
					}
				} ]
			});
		}
	
		function addFun() {
			
				
				var a=$('#zjpzAddGrid').datagrid('getRows');
				alert(a);
				$.ajax({  
		            type : "post",  
		            async : false,  
		            url : '${pageContext.request.contextPath}/business/accounting/zJYZPZGL/edit.shtml?jsonString='+JSON.stringify($('#zjpzAddGrid').datagrid("getRows")), 
		            success : function(result) {
		            	var obj = JSON.parse(result);
						if (obj.success) {
							parent.$.messager.alert('提示', obj.msg, 'info');
						}else{
							parent.$.messager.alert('提示', obj.msg, 'info');
						}
						zjpzAddGrid.datagrid('reload');
						parent.$.messager.progress('close');
					}
		        });  
		}
	
		function searchFun() {
			alert("47666678");
			zjpzGrid.datagrid('load', $.serializeObject($('#searchForm')));
		}
		function cleanFun() {
			$('#searchForm input').val('');
			zjpzGrid.datagrid('load', {});
		}
		
	</script>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',title:'',border:false" style="height: 100px; overflow: hidden;" >
			<div>&nbsp;&nbsp;</div>
			<form id="searchForm" action="/business/accounting/zJYZPZGL/dataGrid.shtml" method="post">
				<table class="form_view_border" bordercolordark="#FFFFFF" bordercolorlight="#45b97c" border="px" cellpadding="0" cellspacing="0" style="">
					<tr>
						
						<th class="panel-header"><nobr>核算单位名称:${requestScope.hsdw}</nobr></th>
						
						<th class="panel-header"><nobr>附单据数</nobr></th>
						<td><select  style="width: 156px;height: 27px;"
									class="form_view_input combo easyui-combobox"
									name="CWZT"
									data-options="editable: false">
								<option value="">--请选择--</option>
								<c:forEach items="${requestScope.cwzt }" var="p">
                                 	<option value="${p.ZTDM }">${p.ZTMC }</option>
                               	</c:forEach>
							</select>
						</td>
						<th class="panel-header"><nobr>业务类别</nobr></th>
						<td><select  style="width: 156px;height: 27px;"
								class="form_view_input combo easyui-combobox"
								name="CWZT"
								data-options="editable: false">
								<option value="">--请选择--</option>
								<c:forEach items="${requestScope.kminfo }" var="p">
                                 	<option value="${p.ZTDM }">${p.KMMC }</option>
                               	</c:forEach>
								</select>
						</td>
						<th><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'brick_add',plain:true" onclick="addFun();" >保存</a></th>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false">
				<table id="zjpzAddGrid">
					 <tr>
						<th></th>
					</tr> 
				</table>
		</div>
	</div>

</body>
</html>